package cn.wanda.utils.modelGroup

import cn.wanda.utils.config
import cn.wanda.utils.config.groupJobTrait
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

class group4 extends groupJobTrait {

    def getGroup(sparkSessionContext: SparkSession): DataFrame = {

        val model4TableMap = new config.TableMap
        val model8TableMap = new config.TableMap

        val jdbcDF = sparkSessionContext.sql(group4Sql)
          .rdd
          .map { col => {
              val memberCode = col.getString(0)
              val od_paid_amt_incre_trend = isNull(col.get(1))
              val od_trade_cnt_3mth = isNull(col.get(2))
              val tran_paid_amt_incre_1mth = isNull(col.get(3))
              val max_due = isNull(col.get(4))
              val od_avg_day_12mth = isNull(col.get(5))
              val od_intvl_max = isNull(col.get(6))
              val od_paid_ratio_12mth = isNull(col.get(7))
              val od_tran_cnt_1mth = isNull(col.get(8))
              val od_paid_amt_prt_12mth = isNull(col.get(9))
              val tran_paid_amt_prt_12mth = isNull(col.get(10))

              var od_paid_amt_incre_trend_goal = 0f
              var od_trade_cnt_3mth_goal = 0f
              var tran_paid_amt_incre_1mth_goal = 0f
              var max_due_goal = 0f
              var od_avg_day_12mth_goal = 0f
              var od_intvl_max_goal = 0f
              var od_paid_ratio_12mth_goal = 0f
              var od_tran_cnt_1mth_goal = 0f
              var od_paid_amt_prt_12mth_goal = 0f
              var tran_paid_amt_prt_12mth_goal = 0f

              if (od_paid_amt_incre_trend < 0f) {
                  od_paid_amt_incre_trend_goal = -10.8f
              } else if (od_paid_amt_incre_trend == 0f) {
                  od_paid_amt_incre_trend_goal = -0.5f
              } else if (od_paid_amt_incre_trend <= 0.6f) {
                  od_paid_amt_incre_trend_goal = 9.5f
              } else {
                  od_paid_amt_incre_trend_goal = 24.0f
              }

              if (od_trade_cnt_3mth <= 0f) {
                  od_trade_cnt_3mth_goal = 13.9f
              } else if (od_trade_cnt_3mth == 1f) {
                  od_trade_cnt_3mth_goal = -1.4f
              } else if (od_trade_cnt_3mth <= 3f) {
                  od_trade_cnt_3mth_goal = -15.8f
              } else {
                  od_trade_cnt_3mth_goal = -29.4f
              }

              if (tran_paid_amt_incre_1mth < 0f) {
                  tran_paid_amt_incre_1mth_goal = 47.4f
              } else if (tran_paid_amt_incre_1mth < 0.3f) {
                  tran_paid_amt_incre_1mth_goal = -22.8f
              } else if (tran_paid_amt_incre_1mth <= 0.6f) {
                  tran_paid_amt_incre_1mth_goal = 3.2f
              } else if (tran_paid_amt_incre_1mth <= 0.9f) {
                  tran_paid_amt_incre_1mth_goal = 15.9f
              } else {
                  tran_paid_amt_incre_1mth_goal = 30.4f
              }

              if (max_due <= 13f) {
                  max_due_goal = 24.1f
              } else if (max_due <= 20f) {
                  max_due_goal = -1.3f
              } else {
                  max_due_goal = -19.9f
              }

              if (od_avg_day_12mth <= 3f) {
                  od_avg_day_12mth_goal = 32.9f
              } else if (od_avg_day_12mth <= 5f) {
                  od_avg_day_12mth_goal = 18.8f
              } else if (od_avg_day_12mth <= 8.5f) {
                  od_avg_day_12mth_goal = -0.2f
              } else {
                  od_avg_day_12mth_goal = -28.2f
              }

              if (od_intvl_max <= 31f) {
                  od_intvl_max_goal = -22.0f
              } else if (od_intvl_max <= 61f) {
                  od_intvl_max_goal = 6.2f
              } else {
                  od_intvl_max_goal = 7.4f
              }

              if (od_paid_ratio_12mth <= 0.15f) {
                  od_paid_ratio_12mth_goal = -41.4f
              } else if (od_paid_ratio_12mth <= 0.4f) {
                  od_paid_ratio_12mth_goal = -1.1f
              } else {
                  od_paid_ratio_12mth_goal = 23.4f
              }

              if (od_tran_cnt_1mth <= 1f) {
                  od_tran_cnt_1mth_goal = 64.6f
              } else if (od_tran_cnt_1mth <= 2f) {
                  od_tran_cnt_1mth_goal = 9.2f
              } else if (od_tran_cnt_1mth <= 4f) {
                  od_tran_cnt_1mth_goal = -19.2f
              } else {
                  od_tran_cnt_1mth_goal = -65.76f
              }

              val model4Score: Float = 500.00f + od_paid_amt_incre_trend_goal + od_trade_cnt_3mth_goal + tran_paid_amt_incre_1mth_goal + max_due_goal + od_avg_day_12mth_goal + od_intvl_max_goal + od_paid_ratio_12mth_goal + od_tran_cnt_1mth_goal

              model4TableMap.member_code = memberCode
              model4TableMap.od_paid_amt_incre_trend_value = od_paid_amt_incre_trend
              model4TableMap.od_trade_cnt_3mth_value = od_trade_cnt_3mth
              model4TableMap.tran_paid_amt_incre_1mth_value = tran_paid_amt_incre_1mth
              model4TableMap.max_due_value = max_due
              model4TableMap.od_avg_day_12mth_value = od_avg_day_12mth
              model4TableMap.od_intvl_max_value = od_intvl_max
              model4TableMap.od_paid_ratio_12mth_value = od_paid_ratio_12mth
              model4TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model4TableMap.od_paid_amt_incre_trend_goal = od_paid_amt_incre_trend_goal
              model4TableMap.od_trade_cnt_3mth_goal = od_trade_cnt_3mth_goal
              model4TableMap.tran_paid_amt_incre_1mth_goal = tran_paid_amt_incre_1mth_goal
              model4TableMap.max_due_goal = max_due_goal
              model4TableMap.od_avg_day_12mth_goal = od_avg_day_12mth_goal
              model4TableMap.od_intvl_max_goal = od_intvl_max_goal
              model4TableMap.od_paid_ratio_12mth_goal = od_paid_ratio_12mth_goal
              model4TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_goal
              model4TableMap.model_of_the_total_score = model4Score

              val model4Map: config.TableMap = model4TableMap

              if (od_paid_amt_prt_12mth < 0.12f) {
                  od_paid_amt_prt_12mth_goal = -59.6f
              } else if (od_paid_amt_prt_12mth <= 0.22f) {
                  od_paid_amt_prt_12mth_goal = -15.3f
              } else if (od_paid_amt_prt_12mth <= 0.32f) {
                  od_paid_amt_prt_12mth_goal = 4.8f
              } else if (od_paid_amt_prt_12mth <= 0.52f) {
                  od_paid_amt_prt_12mth_goal = 22.9f
              } else {
                  od_paid_amt_prt_12mth_goal = 45.1f
              }

              if (tran_paid_amt_prt_12mth <= 0.87f) {
                  tran_paid_amt_prt_12mth_goal = -22.2f
              } else if (tran_paid_amt_prt_12mth <= 0.93f) {
                  tran_paid_amt_prt_12mth_goal = -8.0f
              } else if (tran_paid_amt_prt_12mth <= 0.97f) {
                  tran_paid_amt_prt_12mth_goal = 13.8f
              } else {
                  tran_paid_amt_prt_12mth_goal = 36.8f
              }

              if (tran_paid_amt_incre_1mth < 0f) {
                  tran_paid_amt_incre_1mth_goal = 31.2f
              } else if (tran_paid_amt_incre_1mth == 0f) {
                  tran_paid_amt_incre_1mth_goal = -13.1f
              } else if (tran_paid_amt_incre_1mth <= 0.3f) {
                  tran_paid_amt_incre_1mth_goal = -29.2f
              } else if (tran_paid_amt_incre_1mth <= 0.6f) {
                  tran_paid_amt_incre_1mth_goal = 3.0f
              } else if (tran_paid_amt_incre_1mth <= 0.9f) {
                  tran_paid_amt_incre_1mth_goal = 15.4f
              } else {
                  tran_paid_amt_incre_1mth_goal = 26.1f
              }

              if (max_due <= 13f) {
                  max_due_goal = 33.3f
              } else if (max_due <= 16f) {
                  max_due_goal = 8.0f
              } else if (max_due <= 30f) {
                  max_due_goal = -15.2f
              } else {
                  max_due_goal = -32.6f
              }

              if (od_avg_day_12mth <= 4f) {
                  od_avg_day_12mth_goal = 23.8f
              } else if (od_avg_day_12mth <= 5.5f) {
                  od_avg_day_12mth_goal = 12.7f
              } else if (od_avg_day_12mth <= 8.5f) {
                  od_avg_day_12mth_goal = -0.5f
              } else if (od_avg_day_12mth <= 12f) {
                  od_avg_day_12mth_goal = -13.5f
              } else {
                  od_avg_day_12mth_goal = -27.0f
              }

              if (od_intvl_max <= 31f) {
                  od_intvl_max_goal = -15.6f
              } else if (od_intvl_max <= 62f) {
                  od_intvl_max_goal = 5.7f
              } else {
                  od_intvl_max_goal = 5.4f
              }

              if (od_tran_cnt_1mth <= 1f) {
                  od_tran_cnt_1mth_goal = 63.4f
              } else if (od_tran_cnt_1mth <= 2f) {
                  od_tran_cnt_1mth_goal = 8.0f
              } else if (od_tran_cnt_1mth <= 4f) {
                  od_tran_cnt_1mth_goal = -14.5f
              } else {
                  od_tran_cnt_1mth_goal = -52.3f
              }

              val model8Score: Float = 500.00f + od_paid_amt_prt_12mth_goal + tran_paid_amt_prt_12mth_goal + tran_paid_amt_incre_1mth_goal + max_due_goal + od_avg_day_12mth_goal + od_intvl_max_goal + od_tran_cnt_1mth_goal

              model8TableMap.member_code = memberCode
              model8TableMap.od_paid_amt_prt_12mth_value = od_paid_amt_prt_12mth
              model8TableMap.tran_paid_amt_prt_12mth_value = tran_paid_amt_prt_12mth
              model8TableMap.tran_paid_amt_incre_1mth_value = tran_paid_amt_incre_1mth
              model8TableMap.max_due_value = max_due
              model8TableMap.od_avg_day_12mth_value = od_avg_day_12mth
              model8TableMap.od_intvl_max_value = od_intvl_max
              model8TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model8TableMap.od_paid_amt_prt_12mth_goal = od_paid_amt_prt_12mth_goal
              model8TableMap.tran_paid_amt_prt_12mth_goal = tran_paid_amt_prt_12mth_goal
              model8TableMap.tran_paid_amt_incre_1mth_goal = tran_paid_amt_incre_1mth_goal
              model8TableMap.max_due_goal = max_due_goal
              model8TableMap.od_avg_day_12mth_goal = od_avg_day_12mth_goal
              model8TableMap.od_intvl_max_goal = od_intvl_max_goal
              model8TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_goal
              model8TableMap.model_of_the_total_score = model8Score

              val model8Map: config.TableMap = model8TableMap

              (model4Map, model8Map)
          }
          }

        val model4Rdd = jdbcDF.map(_._1).map(
            x => Row(x.data_date, 4, x.member_code, null, null, null, null, null, null, null,
                null, null, null, x.tran_paid_amt_incre_1mth_value, null, null, null, x.od_intvl_max_value, x.od_tran_cnt_1mth_value, x.od_avg_day_12mth_value,
                x.od_paid_amt_incre_trend_value, x.od_tran_cnt_1mth_value, x.max_due_value, x.od_trade_cnt_3mth_value, x.od_paid_ratio_12mth_value, null, null, null, null, null,
                null, null, null, null, null, null, x.tran_paid_amt_incre_1mth_goal, null, null, null,
                x.od_intvl_max_goal, x.od_tran_cnt_1mth_goal, x.od_avg_day_12mth_goal, x.od_paid_amt_incre_trend_goal, null, x.max_due_goal, x.od_trade_cnt_3mth_goal, x.od_paid_ratio_12mth_goal, null, x.model_of_the_total_score))

        val model4DF = sparkSessionContext.sqlContext.createDataFrame(model4Rdd, schema)

        val model8Rdd = jdbcDF.map(_._2).map(
            x => Row(x.data_date, 8, x.member_code, null, null, null, null, null, null, null,
                null, null, null, x.tran_paid_amt_incre_1mth_value, x.tran_paid_amt_prt_12mth_value, null, null, x.od_intvl_max_value, x.od_tran_cnt_1mth_value, x.od_avg_day_12mth_value,
                null, x.od_tran_cnt_1mth_value, x.max_due_value, null, null, x.od_paid_amt_prt_12mth_value, null, null, null, null,
                null, null, null, null, null, null, x.tran_paid_amt_incre_1mth_goal, x.tran_paid_amt_prt_12mth_goal, null, null,
                x.od_intvl_max_goal, x.od_tran_cnt_1mth_goal, x.od_avg_day_12mth_goal, null, null, x.max_due_goal, null, null, x.od_paid_amt_prt_12mth_goal, x.model_of_the_total_score))

        val model88DF = sparkSessionContext.sqlContext.createDataFrame(model8Rdd, schema)
        model4DF.union(model88DF)
    }
}
